package com.telnetnz.ticketdesk.action.acp.log;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.rockbb.common.simpleframe.config.SimpleConfig;
import com.rockbb.common.sso.session.SessionConstants;
import com.rockbb.common.utilities.text.ComponentBuilder;
import com.rockbb.common.utilities.text.StringUtils;
import com.rockbb.common.utilities.time.TimeUtil;
import com.telnetnz.ticketdesk.action.base.BasicSessionPageAcpAction;
import com.telnetnz.ticketdesk.action.common.CommonUtils;
import com.telnetnz.ticketdesk.bean.OperationLog;
import com.telnetnz.ticketdesk.service.OperationLogService;
import com.telnetnz.ticketdesk.service.UserService;

public class OperationLogAction extends BasicSessionPageAcpAction
{
	public String doLanding()
	{
		ListRequestBean bean = new ListRequestBean();

		List<OperationLog> operationlogs = OperationLogService.getLogsByPageAndCondition(
				bean.getStart(), 
				bean.getLimit(), 
				bean.getOrderby(), 
				0, 
				bean.getUserId(), 
				bean.getObjectType(), 
				bean.getObjectId(), 
				bean.getAction(), 
				bean.getCreateAfter(), 
				bean.getCreateBefore());

		String page_nav = ComponentBuilder.generate_pagination(
				baseLink() + bean.getParams(), 
				"start", 
				bean.getSize(), 
				bean.getLimit(), 
				bean.getStart(), 
				false, 
				"");

		this.put_page_key("operationlogs", operationlogs);
		this.put_page_key("page_nav", page_nav);
		this.put_page_key("bean", bean);

		this.put_page_key("orderby_options", CommonUtils.buildOrderByDropdown(langRes, "log.orderby.", OperationLogService.ORDER_BY, bean.getOrderby()));
		this.put_page_key("maintainer_options", CommonUtils.buildUserDropdown(
				UserService.getByPageAndCondition(0, 100, UserService.ORDER_BY_NAME, 1, SessionConstants.USER_ID_ANONYMOUS, null, null),
				bean.getUserId(), 
				true));
		this.put_page_key("type_options", CommonUtils.buildSingleDropdown(langRes, "log.type.", OperationLogService.OBJECT_TYPES, bean.getObjectType(), true));
		this.put_page_key("post_action", baseLink());
		this.put_js_key("source_files", new String[] {
				SimpleConfig.getConfig("static_file_url")+"/js/jquery-ui.min.js",
				SimpleConfig.getConfig("static_file_url")+"/js/jquery-ui-timepicker-addon.js"
				});

		this.put_css_key("source_files", new String[] {
				SimpleConfig.getConfig("static_file_url")+"/js/jquery-ui.css"});

		return SUCCESS;
	}

	public class ListRequestBean extends RequestBean
	{

		private int start;
		private int limit;
		private int orderby;
		private long userId;
		private int objectType;
		private long objectId;
		private Date createAfter;
		private Date createBefore;
		private String action;
		private long size;
		private String params;

		public int getStart() {
			return start;
		}

		public int getLimit() {
			return limit;
		}

		public int getOrderby() {
			return orderby;
		}

		public long getUserId() {
			return userId;
		}

		public int getObjectType() {
			return objectType;
		}

		public long getObjectId() {
			return objectId;
		}

		public Date getCreateAfter() {
			return createAfter;
		}

		public Date getCreateBefore() {
			return createBefore;
		}

		public String getAction() {
			return action;
		}

		public long getSize() {
			return size;
		}

		public String getParams() {
			return params;
		}

		@Override
		protected void loadRequest()
		{
			start = getInt("start");
			limit = getInt("limit", 20);
			orderby = getInt("orderby");
			userId = getLong("user_id", 0L);
			objectType = getInt("obj_type",-1);
			objectId = getLong("obj_id",0L);
			if (get("create_before").length() > 0) 
				createBefore = TimeUtil.getDate(get("create_before"),"yyyy-MM-dd");
			if (get("create_after").length() > 0) 
				createAfter = TimeUtil.getDate(get("create_after"),"yyyy-MM-dd");
			action= getUnicode("action", SimpleConfig.getConfig("encoding"));

			size = OperationLogService.countByCondition(
					userId, objectType, objectId, action, createAfter, createBefore);

			List<String[]> nv_pairs = new ArrayList<String[]>();
			nv_pairs.add(new String[]{"limit", limit+""});
			nv_pairs.add(new String[]{"orderby", orderby+""});
			if (userId > 0L)
				nv_pairs.add(new String[]{"user_id", userId+""});
			if (objectType > -1)
				nv_pairs.add(new String[]{"obj_type", objectType+""});
			if (objectId > 0L)
				nv_pairs.add(new String[]{"obj_id", objectId+""});
			if (action.length() > 0)
				nv_pairs.add(new String[]{"action", action});
			if (createAfter != null)
				nv_pairs.add(new String[]{"create_after", get("create_after")});
			if (createBefore != null)
				nv_pairs.add(new String[]{"create_before", get("create_before")});

			params = StringUtils.appendParametersToURL("", nv_pairs, SimpleConfig.getConfig("encoding"));
		}

		@Override
		protected boolean validate()
		{
			return false;
		}
	}

}
